package c.w.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

public class ConfigFactory {

		public static void getOnePropertiesAllMessage(String path, String fileName) {
			FileInputStream inStream = null;
			Properties pro = null;
			
			try {
				
				inStream = new FileInputStream(path + File.separator + fileName);

				pro = new Properties();
				
				pro.load(inStream);
				
				inStream.close();
				
				inStream = null;
				
				Enumeration<?> en = pro.propertyNames();
				
				while(en.hasMoreElements()) {
					String key = (String) en.nextElement();
					String value = pro.getProperty(key);
					System.out.println(key +" = " + value);
				}
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		public static String getPropertiesValueByKey(String path, String fileName, String key) {
			
			InputStream inStream = null;
			Properties pro = null;
			
			String value = "";
			
			String filePath = path + File.separator + fileName;
			
			try {
				
				inStream = new FileInputStream(filePath);
				
				pro = new Properties();
				
				pro.load(inStream);
				
				if (pro.containsKey(key)) {
					value = pro.getProperty(key);
				} else {
				}
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally{
				if(inStream != null) {
					try {
						inStream.close();
						inStream = null;
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
			return value;
		}
		
		
		public static void saveOneMessageToProperties(String key, String value, String path, String fileName) {
			FileInputStream inStream = null;
			FileOutputStream outStream = null;
			
			Properties pro = null;
			
			String filePath = path + File.separator + fileName;
			
			try {
				
				inStream = new FileInputStream(filePath);
				
				pro = new Properties();
				
				pro.load(inStream);
				
				pro.setProperty(key, value);
				
				outStream = new FileOutputStream(filePath);
				
				pro.store(outStream, "this is change "+fileName+" port");
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if(inStream != null) {
					try {
						inStream.close();
						inStream = null;
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (outStream != null) {
					try {
						outStream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
		}
}
